﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using FluentNHibernate.Cfg;
using FluentNHibernate.Cfg.Db;
using NHibernate.Cfg;
using NHibernate.Tool.hbm2ddl;
using NHibernate;
using NHibernate.Linq;

namespace Chapter2
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        const string connstring = "server=SUJIE-PC\\SQLExpress;"+
            "database=NHibernateBeginnerGuide;"+
            "Integrated Security=True";
        public MainWindow()
        {
            InitializeComponent();
        }

        private void btnCreateDatabase_Click(object sender, RoutedEventArgs e)
        {
            Fluently.Configure()
                .Database(MsSqlConfiguration
                .MsSql2008
                .ConnectionString(connstring))
                .Mappings(m=>m.FluentMappings
                    .AddFromAssemblyOf<ProductMap>())
                    .ExposeConfiguration(CreateSchema)
                    .BuildConfiguration();
        }

        private static void CreateSchema(Configuration cfg)
        {
            var schemaExport=new SchemaExport(cfg);
           schemaExport.Drop(false,true);
            schemaExport.Create(false,true);
        }

        private void btnCreateSessionFactory_Click(object sender, RoutedEventArgs e)
        {
            var factory = CreateSessionFactory();
        }

        private ISessionFactory CreateSessionFactory()
        {
            return Fluently.Configure()
                .Database(MsSqlConfiguration
                .MsSql2008
                .ConnectionString(connstring))
                .Mappings(m => m.FluentMappings
                .AddFromAssemblyOf<ProductMap>())
                .BuildSessionFactory();
        }

        private void btnCreateSession_Click(object sender, RoutedEventArgs e)
        {
            var factory = CreateSessionFactory();
            using(var session=factory.OpenSession())
            {
                int i = 0;
                i = 1;
            }
        }

        private void btnAddCategory_Click(object sender, RoutedEventArgs e)
        {
            var factory = CreateSessionFactory();
            using(var session=factory.OpenSession())
            {
                var category = new Category
                {
                    Name=tbCName.Text,
                    Description=tbCDescription.Text
                
                };
                var id= session.Save(category);
            }
        }

        private void btnDeleteCategory_Click(object sender, RoutedEventArgs e)
        {
            var factory = CreateSessionFactory();
            using (var session = factory.OpenSession())
            {
                ITransaction tx1 = session.BeginTransaction();
                var category = session.Get<Category>(5);
                session.Delete(category);   //计划执行delete
                tx1.Commit();   //清理缓存，执行delete语句
            }
        }

        private void btnAddProduct_Click(object sender, RoutedEventArgs e)
        {
            var factory = CreateSessionFactory();
            using (var session = factory.OpenSession())
            {
                var category = new Category
                {
                    Name = tbCName.Text,
                    Description = tbCDescription.Text

                };
                var product = new Product
                {
                    Name = tbPName.Text,
                    Category = category
                };
                session.Save(category);
                session.Save(product);
                    
            }
        }

        private void btnLoadCategories_Click(object sender, RoutedEventArgs e)
        {
            var factory = CreateSessionFactory();
            using(var session=factory.OpenSession())
            {
                var categories = session.Query<Category>()
                    .OrderBy(c => c.Name)
                    .ToList();
                lbCategories.ItemsSource = categories;
                lbCategories.DisplayMemberPath = "Name";
                
            }
        }



    }
}
